<template>
  <div class="permission-container">
    <div class="app-container">
      <!-- 表格 -->
      <el-card>
        <div style="text-align: right; margin-bottom: 20px">
          <el-button type="primary" size="small" @click="showAdd(1,'0')">添加权限</el-button>
        </div>
        <el-table border :data="list" row-key="id">
          <el-table-column label="名称" prop="name" />
          <el-table-column label="标识" prop="code" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作">
            <template v-slot="{row}">
              <el-button v-if="row.type === 1" size="small" type="text" @click="showAdd(2 , row.id)">添加权限点</el-button>
              <el-button size="small" type="text" @click="look(row.id)">查看</el-button>
              <el-button size="small" type="text" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </div>

    <!-- 新增编辑的弹层 -->
    <el-dialog :visible.sync="showDialog" width="40%" :title="formData.id ? '编辑权限': '新增权限'" @close="close">
      <!-- 表单内容 -->
      <el-form label-width="100px" :model="formData">
        <el-form-item label="权限名称" prop="name">
          <el-input v-model="formData.name" />
        </el-form-item>
        <el-form-item label="权限标识" prop="code">
          <el-input v-model="formData.code" />
        </el-form-item>
        <el-form-item label="权限描述" prop="description">
          <el-input v-model="formData.description" />
        </el-form-item>
        <el-form-item label="权限启用" prop="enVisible">
          <el-switch
            v-model="formData.enVisible"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-value="0"
            inactive-value="1"
          />
        </el-form-item>
      </el-form>

      <template #footer>
        <div style="text-align: right;">
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="clickadd">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList, delPermission, addPermission, getPermissionDetail, updatePermission } from '@/api/permission'
import { listToTree } from '@/utils/index'
export default {
  name: 'Permission',
  data() {
    return {
      showDialog: false,
      list: [],
      formData: {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      },
      rules: {
        name: [{ required: true, message: '权限名称不能为空', trigger: ['change', 'blur'] }],
        code: [{ required: true, message: '权限标识不能为空', trigger: ['change', 'blur'] }]
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      const res = await getPermissionList()

      this.list = listToTree(res, '0')
    },
    // 删除权限点
    async del(id) {
      await delPermission(id)
      this.$message.success('删除权限点成功')
      this.getPermissionList()
    },
    // 展示添加权限点
    showAdd(type, id) {
      this.formData.pid = id
      this.formData.type = type
      this.showDialog = true
    },

    // 关闭添加权限点
    close() {
      this.showDialog = false
      this.getPermissionList()
      this.formData = {
        enVisible: '0', // 开启
        name: '', // 名称
        code: '', // 权限标识
        description: '', // 描述
        type: '', // 类型
        pid: '' // 添加到哪个节点下
      }
    },
    // 确认添加权限点
    async clickadd() {
      if (this.formData.id) {
        await updatePermission(this.formData)
      } else {
        await addPermission(this.formData)
      }

      this.$message.success('操作成功')
      this.close()
    },
    // 编辑,并回显权限点
    async look(id) {
      const res = await getPermissionDetail(id)
      this.formData = { ...res }
      this.showDialog = true
    }
  }
}
</script>
